package com.abaaba.project2.configuration;

import com.abaaba.project2.shiro.realm.CustomRealm;
import com.abaaba.project2.shiro.sessionmanager.CustomSessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfiguration {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        //认证失败后重定向的url,前后端分离项目不需要，因为认证失败也是返回Json
//        shiroFilterFactoryBean.setLoginUrl("/unAuthc");
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //最重要的事情
        //对请求过滤 filter
        //key 请求url value对应的是过滤器
        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/admin/auth/login","anon");
        filterMap.put("/wx/auth/login","anon");
        filterMap.put("/wx/home/index","anon");
        filterMap.put("/wx/goods/*","anon");
        filterMap.put("/wx/catalog/*","anon");
        filterMap.put("/wx/comment/*","anon");
        filterMap.put("/wx/home/*","anon");
        filterMap.put("/wx/storage/upload","anon");
        filterMap.put("/wx/feedback/submit","anon");
        filterMap.put("/wx/storage/fetch/*","anon");

        filterMap.put("/wx/brand/*","anon");

        filterMap.put("/wx/search/helper","anon");
        filterMap.put("/wx/search/index","anon");
        filterMap.put("/wx/auth/regCaptcha","anon");
        filterMap.put("/wx/auth/register","anon");
        filterMap.put("/wx/auth/reset","anon");
        //当你分配了perm1的权限时才能访问need/perm这请求
        //filterMap.put("/need/perm","perms[perm1]");
        //优选的方式是声明式
        //filterMap.put("/auth/logout","logout");
        filterMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm customRealm, DefaultWebSessionManager webSessionManager){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(customRealm);
        defaultWebSecurityManager.setSessionManager(webSessionManager);
        return defaultWebSecurityManager;
    }

    /*
     * 声明式鉴权 注解需要的组件
     * */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManagerz){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManagerz);
        return authorizationAttributeSourceAdvisor;
    }

    /*使用映射处理异常：key为异常全类名 value为异常处理的请求*/
    /*@Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        Properties mappings = new Properties();
        mappings.put("org.apache.shiro.authz.AuthorizationException","/noperm");
        simpleMappingExceptionResolver.setExceptionMappings(mappings);
        return simpleMappingExceptionResolver;
    }*/

    @Bean
    public DefaultWebSessionManager webSessionManager(){
        CustomSessionManager customSessionManager = new CustomSessionManager();
        return customSessionManager;
    }

}
